﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using Maticsoft.Common;

namespace Maticsoft.Web.Admin.Shopping
{
    public partial class ProductEdit : PageBase
    {
        int _Id;
        BLL.Products _ProBLL = new BLL.Products();
        protected void Page_Load(object sender, EventArgs e)
        {
            Check("shopping", "read");
            int.TryParse(Request.QueryString["id"], out _Id);
            if (!IsPostBack)
            {
                LoadList();
                GetData();
            }
        }

        protected void GetData()
        {
            if (_Id > 0)
            {
                Model.Products proModel = _ProBLL.GetModel(_Id);
                txtProductName.Text = proModel.ProductName;
                ddlType.SelectedValue = proModel.CategoryId.ToString();
                txtProductNo.Text = proModel.ProductNo;
                txtFactoryNo.Text = proModel.FactoryNo;
                txtLayout.Text = proModel.Layout;
                txtSize.Text = proModel.Size;
                txtSalePrice.Text = Convert.ToString(proModel.SalePrice);
                txtMarketPrice.Text = Convert.ToString(proModel.MarketPrice);
                txtPulisher.Text = proModel.Publisher;
                txtStorer.Text = proModel.Storer;
                txtShipFeeDesc.Text = proModel.ShipFeeDesc;
                txtShipFee.Text = Convert.ToString(proModel.ShipFee);
                txtTaxFee.Text = Convert.ToString(proModel.TaxFee);
                txtInsureFee.Text = Convert.ToString(proModel.InsureFee);
                txtOrderNo.Text = Convert.ToString(proModel.OrderNo);
                txtStoreNum.Text = Convert.ToString(proModel.StoreNum);
                txtMaxNum.Text = Convert.ToString(proModel.MaxNum);
                txtMinNum.Text = Convert.ToString(proModel.MinNum);
                txtWeight.Text = Convert.ToString(proModel.Weight);
                FCKeditor1.Value = proModel.Summary;
                FCKeditor2.Value = proModel.Detail;
                FCKeditor3.Value = proModel.DetailWap;
                Image1.ImageUrl = "~/uploadpic/" + proModel.Pic;
                cbHot.Checked = proModel.IsHot == 1 ? true : false;
                cbGood.Checked = proModel.IsGood == 1 ? true : false;
                cbHotSale.Checked = proModel.IsHotSale == 1 ? true : false;
                cbTop.Checked = proModel.IsTop == 1 ? true : false;
            }
        }

        /// <summary>
        /// 加载整个分类表
        /// </summary>
        private void LoadList()
        {
            int MTcount = 0;//执行遍历的次数
            //开始迭代加载
            MakeTr("0", MTcount);

        }
        /// <summary>
        /// 装配每一行
        /// </summary>
        /// <param name="id">上级ID</param>
        /// <param name="count">迭代次数</param>
        private void MakeTr(string id, int count)
        {
            BLL.Categorys chaBLL = new BLL.Categorys();
            DataView dv = new DataView(chaBLL.GetList("parentID=" + id + " order by CategoryId asc").Tables[0]);
            foreach (DataRowView drv in dv)
            {
                //形成下拉列表
                ListItem li = new ListItem(MakeFelgefu(count) + drv["CategoryName"].ToString(), drv["CategoryId"].ToString());
                ddlType.Items.Add(li);

                //再次遍历
                MakeTr(drv["CategoryId"].ToString(), count + 1);
            }

        }
        /// <summary>
        /// Make分隔符
        /// </summary>
        /// <param name="count"></param>
        /// <returns></returns>
        private string MakeFelgefu(int count)
        {
            string Returnwords = string.Empty;
            if (count == 0)
            {
                Returnwords = "";
            }

            else
            {
                Returnwords = ("┠").PadLeft(count, '　');
            }
            return Returnwords;
        }

        protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
        {
            Model.Products proModel = _ProBLL.GetModel(_Id);
            if (proModel == null)
            {
                proModel = new Model.Products();
                proModel.CreateTime = DateTime.Now;
                proModel.UpdateTime = DateTime.Now;
                proModel.IsActive = 1;
            }
            proModel.ProductName = txtProductName.Text;
            proModel.CategoryId = int.Parse(ddlType.SelectedValue);
            proModel.ProductNo=txtProductNo.Text;
            proModel.FactoryNo = txtFactoryNo.Text.Trim();
            proModel.Layout=txtLayout.Text;
            proModel.Size=txtSize.Text;
            proModel.SalePrice=decimal.Parse(txtSalePrice.Text);
            proModel.MarketPrice = decimal.Parse(txtMarketPrice.Text);
            proModel.Publisher=txtPulisher.Text;
            proModel.Storer=txtStorer.Text;
            proModel.ShipFeeDesc=txtShipFeeDesc.Text;
            proModel.ShipFee=decimal.Parse(txtShipFee.Text);
            proModel.TaxFee=decimal.Parse(txtTaxFee.Text);
            proModel.InsureFee=decimal.Parse(txtInsureFee.Text);
            proModel.OrderNo = int.Parse(txtOrderNo.Text);
            proModel.StoreNum = int.Parse(txtStoreNum.Text);
            proModel.MaxNum=int.Parse(txtMaxNum.Text);
            proModel.MinNum=int.Parse(txtMinNum.Text);
            proModel.Summary = FCKeditor1.Value;
            proModel.Detail = FCKeditor2.Value;
            proModel.DetailWap = FCKeditor3.Value;
            proModel.Weight = int.Parse(txtWeight.Text.Trim());
            proModel.IsHot = (cbHot.Checked ? 1 : 0);
            proModel.IsGood = (cbGood.Checked ? 1 : 0);
            proModel.IsHotSale = (cbHotSale.Checked ? 1 : 0);
            proModel.IsTop = (cbTop.Checked ? 1 : 0);
            if (File1.Value != "")
            {
                ImageUpload img = new ImageUpload();
                img.SavePath = "~/uploadpic/";
                img.FormFile = File1;
                img.Upload();
                if (img.Error == 0)
                {
                    proModel.Pic = img.OutFileName;
                }
            }
            string logContent = string.Empty;
            if (_Id > 0)
            {
                proModel.UpdateTime = DateTime.Now;
                _ProBLL.Update(proModel);
                logContent = ManagerName + " 修改产品，编号：" + _Id;
            }
            else
            {
                _Id = _ProBLL.Add(proModel);
                logContent = ManagerName + " 添加产品，编号：" + _Id;
            }

            BLL.Log logBLL = new BLL.Log();
            Model.Log logModel = new Model.Log();
            logModel.LogTitleId = 3;
            logModel.ObjectId = _Id;
            logModel.UserId = int.Parse(UserId);
            logModel.ClientIP = IPHelper.GetClientIP();
            logModel.LogDesc = logContent;
            logModel.CreateTime = DateTime.Now;
            logBLL.Add(logModel);

            MessageBox.ShowAndRedirect(this.Page, "操作成功", "ProductEdit.aspx");
        }
    }
}